export const isPhoneAvailable = (pone) => {
	var myreg = /^1[3456789]\d{9}$/
	if (!myreg.test(pone)) {
		return false;
	} else {
		return true;
	}
}
export const comMa = (num = 0) => {
	let result = '';

	let numArr = num.toString().split('.');
	let int = numArr[0].toString().split('').reverse();
	let decmial = numArr[1] ? '.' + numArr[1] : '';
	if (int.length > 4) {
		let n = int.length - 1
		for (n; n >= 0; n--) {
			result += int[n];
			if (n % 3 === 0 && n > 0) {
				result += ','
			}
		}
		result = result + decmial
	} else {
		result = num
	}
	return result;
}
export const isAvailableEmail = (sEmail) => {
	var reg = /^([\w+\.])+@\w+([.]\w+)+$/
	return reg.test(sEmail)
}
// 当前月份第一天日期
export const CurrentMonthFirst = (date, format) => {
	let m;
	if (!date) {
		date = new Date();
		m = date.getMonth();
	} else {
		m = date.getMonth() - 1;
	}
	let y = date.getFullYear();
	date = new Date(y, m);
	date.setDate(1);
	return getFormat(date, format);
}
// 当前月份最后一天日期
export const CurrentMonthLast = (date, format) => {

	let nextMonth;
	if (!date) {
		date = new Date();
		nextMonth = date.getMonth() + 1;
	} else {
		nextMonth = date.getMonth() - 1;
	}
	var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
	var oneDay = 1000 * 60 * 60 * 24;
	return getFormat(new Date(nextMonthFirstDay - oneDay), format);
}
// 日期格式化
export const getFormat = (timestamp, format) => {
	let Y = timestamp.getFullYear();
	let M =
		timestamp.getMonth() + 1 >= 10 ?
		timestamp.getMonth() + 1 :
		`0${timestamp.getMonth() + 1}`;
	let D =
		timestamp.getDate() >= 10 ? timestamp.getDate() : `0${timestamp.getDate()}`;
	let formatDate;
	switch (format) {
		case "Y":
			formatDate = `${Y}`;
			break;
		case "M":
			formatDate = `${M}`;
			break;
		case "D":
			formatDate = `${D}`;
			break;
		default:
			formatDate = `${Y}-${M}-${D}`;
			break;
	}
	return formatDate;
}

export const getWeek = function() {
	this.nowTime = new Date();
	this.init = function() {
		this.dayInWeek = this.nowTime.getDay();
		this.dayInWeek == 0 && (this.dayInWeek = 7);
		this.thsiWeekFirstDay =
			this.nowTime.getTime() - (this.dayInWeek - 1) * 86400000;
		this.thisWeekLastDay =
			this.nowTime.getTime() + (7 - this.dayInWeek) * 86400000;
		return this;
	};
	this.getWeekType = function(type) {
		type = ~~type;
		var firstDay = this.thsiWeekFirstDay + type * 7 * 86400000;
		var lastDay = this.thisWeekLastDay + type * 7 * 86400000;
		return this.getWeekHtml(firstDay, lastDay);
	};
	this.formateDate = function(time) {
		var newTime = new Date(time);
		var year = newTime.getFullYear();
		var month = newTime.getMonth() + 1;
		var day = newTime.getDate();
		return (
			year +
			"-" +
			(month >= 10 ? month : "0" + month) +
			"-" +
			(day >= 10 ? day : "0" + day)
		);
	};
	this.getWeekHtml = function(f, l) {
		return [this.formateDate(f), this.formateDate(l)];
	};
}


export class DateSet {
	constructor() {
		let date = new Date();
		this.date = date
		this.Y = date.getFullYear();
		this.M = date.getMonth();
		this.D = date.getDate();
		this.nowDate = `${this.Y}-${this.M+1>=10?this.M+1:'0'+(this.M+1)}-${this.D>=10?this.D:'0'+this.D}`
	}
	// 设置指定年月 获取 月末
	MonthEnd(start, num) {
		num = num || 0
		const {
			Y,
			M,
			D
		} = this
		let nextMonth, end_time, nowDateLastDay;
		let oneDay = 24 * 60 * 60 * 1000;
		if (arguments.length > 0) {
			// 求当前月份的最后一天日期
			start = start.split('-');
			nextMonth = new Date(start[0], ~~start[1] - num, 1);
		} else {
			nextMonth = new Date(Y, M + 1, 1).getTime();
		}
		end_time = nextMonth - oneDay;
		nowDateLastDay = new Date(end_time);
		let nowDate = `${Y}-${M + 1 >= 10 ? M + 1 : '0' + (M + 1)}-01`;
		let nowLastDate =
			`${nowDateLastDay.getFullYear()}-${nowDateLastDay.getMonth() + 1 >= 10 ? nowDateLastDay.getMonth() + 1 : '0' + (nowDateLastDay.getMonth() + 1)}`
		return {
			start_time: start ? `${nowLastDate}-01` : nowDate,
			end_time: `${nowLastDate}-${nowDateLastDay.getDate()>=10?nowDateLastDay.getDate():'0'+nowDateLastDay.getDate()}`
		}
	}
	// 设置日期  间隔天数 获取 时间  'start' //获取起始日期  'end' //获取结束日期
	MonthInterval(start, iNum, mode) {
		let modeType = mode || 'start'
		let obj, lastDate, nowDateLastDay, iNumDay;
		if (modeType === 'start') {
			iNumDay = (iNum - 1) * 24 * 60 * 60 * 1000;
			lastDate = new Date(start) - iNumDay
			nowDateLastDay = new Date(lastDate);
			obj = {
				end_time: start,
				start_time: `${nowDateLastDay.getFullYear()}-${nowDateLastDay.getMonth() + 1 >= 10 ? nowDateLastDay.getMonth() + 1 : '0' + (nowDateLastDay.getMonth() + 1)}-${
							nowDateLastDay.getDate() >= 10 ? nowDateLastDay.getDate() : '0' + nowDateLastDay.getDate()
						}`
			}

		} else {
			iNumDay = iNum * 24 * 60 * 60 * 1000;
			lastDate = new Date(start).getTime() + iNumDay
			nowDateLastDay = new Date(lastDate);
			obj = {
				start_time: start,
				end_time: `${nowDateLastDay.getFullYear()}-${nowDateLastDay.getMonth() + 1 >= 10 ? nowDateLastDay.getMonth() + 1 : '0' + (nowDateLastDay.getMonth() + 1)}-${
							nowDateLastDay.getDate() >= 10 ? nowDateLastDay.getDate() : '0' + nowDateLastDay.getDate()
						}`
			}
		}
		return obj
	}
	// 获取当前周 起末
	getNowWeek() {
		const {
			date,
			Y,
			M,
			D
		} = this;
		let nowDay = date.getDay();
		nowDay == 0 && (nowDay = 7)
		let nowDate = `${Y}-${M+1>=10?M+1:'0'+(M+1)}-${D>=10?D:'0'+D}`
		let start, end;
		if (nowDay == 1) {
			start = nowDate
			end = this.MonthInterval(nowDate, 7 - nowDay, 'end').end_time
		} else {
			start = this.MonthInterval(nowDate, nowDay).start_time
			end = this.MonthInterval(nowDate, 7 - nowDay, 'end').end_time
		}
		return {
			start_time: start,
			end_time: end
		}
	}
}
export const AesEncode = (word) => {
	const CryptoJS = require('./crypto-js.min'); //引入crypto
	const key = CryptoJS.enc.Utf8.parse('d887aed2239884eb') // 密钥
	const iv = CryptoJS.enc.Utf8.parse('b2df5feec9d499c9') // 密钥偏移量
	let srcs = CryptoJS.enc.Utf8.parse(word);
	let encrypted = CryptoJS.AES.encrypt(srcs, key, {
		iv: iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	let encryptedBase64Data = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
	return encodeURIComponent(encryptedBase64Data);
}